<?php

class plugin_erling_qd {
    private $is_signed = false;
    private $position = 'usernav_extra3';
    private $position_float_value = [0.99, 0.99];
    private $position_float_color = '#000';
    private $position_float_bgcolor = '#fff';

    public function __construct() {
        require_once DISCUZ_ROOT.'./source/plugin/erling_qd/table/table_erling_qd_configs.php';
        $erling_qd = C::t('erling_qd_configs')->fetch_by_keys([
            'position', 
            // 'position_float_value', 
            // 'position_float_color', 
            // 'position_float_bgcolor'
        ]);
        
        if (!empty($erling_qd['position'])) {
            $this->position = $erling_qd['position']["value"];
            // $float_value_str = $erling_qd['position_float_value']["value"];
            // if (is_string($float_value_str) && strpos($float_value_str, '[') === 0) {
            //     $values = json_decode($float_value_str, true);
            //     $this->position_float_value = array_map(function($value) {
            //         return floatval($value) / 100; // 将百分比值转换为小数
            //     }, $values);
            // } else {
            //     $this->position_float_value = array_map(function($value) {
            //         return floatval($value) / 100; 
            //     }, explode(',', $float_value_str));
            // }
        
            if (!is_array($this->position_float_value) || count($this->position_float_value) != 2) {
                $this->position_float_value = [0.99, 0.99]; 
            }
            $this->position_float_color = $erling_qd['position_float_color']["value"] ??'#000';
            $this->position_float_bgcolor = $erling_qd['position_float_bgcolor']["value"] ??'#fff'; 
        }

        // 是否已签到
        global $_G;
        require_once DISCUZ_ROOT.'./source/plugin/erling_qd/table/table_sings.php';
        $sign_table = C::t('sings');
        $is_signed = $sign_table->is_signed($_G['uid']);
        $this->is_signed = $is_signed;
    }

    public function global_cpnav_extra1(){
        if ($this->position === 'cpnav_extra1') {
            return '<a href="plugin.php?id=erling_qd:sign_in">'.($this->is_signed ? '已签到' : '签到').'</a>';
        }
        
    }

    public function global_header() {
        // 当成浮动入口
        if ($this->position === 'float') {
            if (is_array($this->position_float_value) && count($this->position_float_value) == 2) {
                $style = sprintf(
                    'position: fixed; left: %.2f%%; top: %.2f%%; z-index: 9999; color: %s; background-color: %s; padding: 8px 16px;border-radius: 10px;box-shadow: 0px 0px 3px 1px #7676765e;',
                    floatval($this->position_float_value[0]) * 100,
                    floatval($this->position_float_value[1]) * 100,
                    $this->position_float_color,
                    $this->position_float_bgcolor,
                    $this->position_float_bgcolor,
                );
                return '<div style="'.$style.'"><a href="plugin.php?id=erling_qd:sign_in" style="color:'.$this->position_float_color.';">'.($this->is_signed ? '已签到' : '签到').'</a></div>';
            }
        }
    }

    public function global_footerlink() {
        $position = 'footerlink';
        
        if ($this->position === 'footerlink') {
            return '| <a href="plugin.php?id=erling_qd:sign_in">'.($this->is_signed ? '已签到' : '签到').'</a>';
        }
    }

    public function global_usernav_extra3() {
        $position = 'usernav_extra3';
        
        if ($this->position === 'usernav_extra3') {
            return '<a href="plugin.php?id=erling_qd:sign_in">'.($this->is_signed ? '已签到' : '签到').'</a> |';
        }
    }

    public function global_nav_extra() {
        $position = 'nav_extra';
        if ($this->position === 'nav_extra') {
            return '<ul><li id="mn_forum"><a href="plugin.php?id=erling_qd:sign_in" hidefocus="true">'.($this->is_signed ? '已签到' : '签到').'</a></li></ul>';
        }
    }
}